<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class MessageAction extends BaseApiAction {

	//发送框
	public function dialog() {
		no_debug();
		$this->assign("tweets_id", $_GET["tweets_id"]);
		$this->assign("user_id", $_GET["user_id"]);
		$this->display();
	}

	//发送私信
	public function save() {
		$this->auth();
		$tweets_id = $_POST["tweets_id"];
		$user_id = $_POST["user_id"];
		if ($user_id == $this->uid)
			$this->ajaxReturn("", "不能给自己发私信", 0);
		$content = trim($_POST['content']);
		$tweets = D("Tweets")->where("tweets_id=" . $tweets_id . " and status=1")->find();
		if (!$tweets)
			$this->ajaxReturn(0, "相关主题已被删除，无法再发私信", 0);
		$summary = msubstr($tweets['content'], 0, 30, "utf-8", "...");
		$message = D("Message")->where("tweets_id=" . $tweets_id . " and (user_id=" . $user_id . " or from_id=" . $from_id . ")")->find();
		if ($message) {
			$Reply = D("Reply");
			$data['user_id'] = $user_id;
			$data['message_id'] = $message["message_id"];
			$data['content'] = $content;
			$data['create_time'] = time();
			$Reply->add($data);
		} else {
			$Message = D("Message");
			$data['tweets_id'] = $tweets_id;
			$data['user_id'] = $user_id;
			$data['from_id'] = $this->uid;
			$data['summary'] = $summary;
			$data['content'] = $content;
			$data['from_status'] = 1;
			$data['create_time'] = time();
			$data['last_time'] = time();
			$Message->add($data);
		}
		S("message_new_" . $user_id, null);
		$this->ajaxReturn($data, "发送成功", 1);
	}

	//检查新私信
	public function check() {
		$this->auth();
		$count = D("Message")->cache("message_new_" . $this->uid)->where("(user_id=" . $this->uid . " and status=0) or (from_id=" . $this->uid . " and from_stats=0)")->count();
		$this->ajaxReturn($count, "", 1);
	}

}

?>
